//
//  PersonalHeaderView.m
//  Findif
//
//  Created by Mac on 2019/3/27.
//  Copyright © 2019年 Cicel Chen. All rights reserved.
//

#import "PersonalHeaderView.h"

@interface PersonalHeaderView ()

//头部
@property (nonatomic, weak)UIImageView * topImageView;
@property (nonatomic, weak)UIImageView * iconImageView;
@property (nonatomic, strong)UILabel * nameLabel;
@property (nonatomic, weak)UIImageView * localImageView;
@property (nonatomic, weak)UILabel * localLabel;
@property (nonatomic, strong)UIButton * attentionButton;
@property (nonatomic, strong)UIButton * msgButton;
@property (nonatomic, strong)UILabel * attentionValueLabel;
@property (nonatomic, strong)UILabel * fansValueLabel;
@property (nonatomic, strong)UILabel * attentionLabel;
@property (nonatomic, strong)UILabel * fansLabel;
//相册  动态
@property (nonatomic, weak)UILabel * noAlbumLabel;
@property (nonatomic, strong)UIImageView * oneImageView;
@property (nonatomic, strong)UIImageView * twoImageView;
@property (nonatomic, strong)UIImageView * threeImageView;
@property (nonatomic, weak)UIImageView * albumImageView;
@property (nonatomic, weak)UILabel * albumLabel;
@property (nonatomic, weak)UILabel * albumCountLabel;
@property (nonatomic, weak)UIButton * moreButton;
@property (nonatomic, strong)UIImageView * rightImageView;
@property (nonatomic, weak)UIImageView * dynamicImageView;
@property (nonatomic, weak)UILabel * dynamicLabel;
@property (nonatomic, weak)UIView * lineView;

@end

@implementation PersonalHeaderView

- (instancetype)initWithFrame:(CGRect)frame{
    
    if ([super initWithFrame:frame]) {

        [self setupUI];
    }
    
    return self;
}


- (void)setDict:(NSDictionary *)dict{
    
    _dict = dict;
    
    if([dict[@"head_img"] containsString:@"http"]) {
        
        NSURL * url = [NSURL URLWithString:dict[@"head_img"]];
        [self.iconImageView sd_setImageWithURL:url];
    } else {
        
        NSString * iconStr = [NSString stringWithFormat:@"%@%@",BASEURLSTR,dict[@"head_img"]];
        NSURL * url = [NSURL URLWithString:iconStr];
        [self.iconImageView sd_setImageWithURL:url];
    }
    int is_attention = [dict[@"is_attention"] intValue];
    if (is_attention == 1) {
        //关注
        self.attentionButton.selected = YES;
    }else{
        
        self.attentionButton.selected = NO;
    }
    self.nameLabel.text = dict[@"user_name"];
    self.localLabel.text = dict[@"address"];
//    self.telStr = [NSString stringWithFormat:@"%@",dict[@"phone"]];
    double attention_number = [dict[@"attention_number"] doubleValue];
    double fans_number = [dict[@"fans_number"] doubleValue];
    if (attention_number >= 10000) {
        
        attention_number = attention_number / 10000;
        self.attentionValueLabel.text = [NSString stringWithFormat:@"%.1f万",attention_number];
    }else{
        
        self.attentionValueLabel.text = [NSString stringWithFormat:@"%.0f",attention_number];
    }
    if (fans_number >= 10000) {
        
        fans_number = fans_number / 10000;
        self.fansValueLabel.text = [NSString stringWithFormat:@"%.1f万",fans_number];
    }else{
        
        self.fansValueLabel.text = [NSString stringWithFormat:@"%.0f",fans_number];
    }

}


- (void)setArray:(NSArray *)array{
    
    _array = array;
    
    self.albumCountLabel.text = [NSString stringWithFormat:@"%lu张相片",(unsigned long)self.array.count];
    
    if (array.count == 0) {
        
        self.noAlbumLabel.hidden = NO;
        self.oneImageView.hidden = YES;
        self.twoImageView.hidden = YES;
        self.threeImageView.hidden = YES;
    }else if (array.count == 1){
        
        self.noAlbumLabel.hidden = YES;
        self.oneImageView.hidden = NO;
        NSDictionary * dict = self.array[0];
        NSString * imgStr = [NSString stringWithFormat:@"%@%@",BASEURLSTR,dict[@"img"]];
        NSURL * url = [NSURL URLWithString:imgStr];
        [self.oneImageView sd_setImageWithURL:url];
        self.twoImageView.hidden = YES;
        self.threeImageView.hidden = YES;
    }else if (array.count == 2){
        
        self.noAlbumLabel.hidden = YES;
        self.oneImageView.hidden = NO;
        NSDictionary * dict = self.array[0];
        NSString * imgStr = [NSString stringWithFormat:@"%@%@",BASEURLSTR,dict[@"img"]];
        NSURL * url = [NSURL URLWithString:imgStr];
        [self.oneImageView sd_setImageWithURL:url];
        self.twoImageView.hidden = NO;
        NSDictionary * dictTwo = self.array[1];
        NSString * imgStrTwo = [NSString stringWithFormat:@"%@%@",BASEURLSTR,dictTwo[@"img"]];
        NSURL * urlTwo = [NSURL URLWithString:imgStrTwo];
        [self.twoImageView sd_setImageWithURL:urlTwo];
        self.threeImageView.hidden = YES;
    }else if (array.count >= 3){
        
        self.noAlbumLabel.hidden = YES;
        self.oneImageView.hidden = NO;
        NSDictionary * dict = self.array[0];
        NSString * imgStr = [NSString stringWithFormat:@"%@%@",BASEURLSTR,dict[@"img"]];
        NSURL * url = [NSURL URLWithString:imgStr];
        [self.oneImageView sd_setImageWithURL:url];
        self.twoImageView.hidden = NO;
        NSDictionary * dictTwo = self.array[1];
        NSString * imgStrTwo = [NSString stringWithFormat:@"%@%@",BASEURLSTR,dictTwo[@"img"]];
        NSURL * urlTwo = [NSURL URLWithString:imgStrTwo];
        [self.twoImageView sd_setImageWithURL:urlTwo];
        self.threeImageView.hidden = NO;
        NSDictionary * dictThree = self.array[2];
        NSString * imgStrThree = [NSString stringWithFormat:@"%@%@",BASEURLSTR,dictThree[@"img"]];
        NSURL * urlThree = [NSURL URLWithString:imgStrThree];
        [self.threeImageView sd_setImageWithURL:urlThree];
    }
}


- (void)setCount:(NSString *)count{
    
    _count = count;
    
    self.dynamicCountLabel.text = [NSString stringWithFormat:@"%@个动态",count];
}

- (void)setupUI{
    
    self.userInteractionEnabled = YES;
    
    UIImageView * topImageView = [[UIImageView alloc] initWithFrame:CGRectMake(5 * SCREENH6,  10 * SCREENH6, 365 * SCREENH6, 215 * SCREENH6)];
    
    topImageView.image = [UIImage imageNamed:@"用户信息-背景"];
    topImageView.userInteractionEnabled = YES;
    
    self.topImageView = topImageView;
    
    [self addSubview:self.topImageView];
    
    
    UIImageView * iconImageView = [[UIImageView alloc] init];
    
    iconImageView.image = [UIImage imageNamed:@"用户中心_头像"];
    iconImageView.contentMode = UIViewContentModeScaleAspectFit;
    iconImageView.layer.cornerRadius = 30 * SCREENH6;
    iconImageView.layer.masksToBounds = YES;
    iconImageView.layer.borderWidth = 1.0f;
    iconImageView.layer.borderColor = [ZRWhiteColor CGColor];
    
    self.iconImageView = iconImageView;
    
    [self addSubview:self.iconImageView];
    
    self.nameLabel = [self setupLabelWithTitle:@"孤独患者" WithType:@"big"];
    [self.topImageView addSubview:self.nameLabel];
    
    
    UIImageView * localImageView = [[UIImageView alloc] init];
    
    localImageView.image = [UIImage imageNamed:@"椭圆5"];
    
    self.localImageView = localImageView;
    
    [self.topImageView addSubview:self.localImageView];
    
    UILabel * localLabel = [UILabel new];
    
    localLabel.textColor = ZRBlackColor;
    localLabel.font = [UIFont systemFontOfSize:13 * SCREENH6];
    localLabel.text = @"成都高新区";
    localLabel.textAlignment = NSTextAlignmentCenter;
    
    self.localLabel = localLabel;
    
    [self.topImageView addSubview:self.localLabel];
    
    //    self.attentionButton = [self setupButtonWithTitle:@"关注"];
    //
    self.attentionButton = [UIButton new];
    
    [self.attentionButton setBackgroundImage:[UIImage imageNamed:@"用户信息-加关注"] forState:UIControlStateNormal];
    [self.attentionButton setBackgroundImage:[UIImage imageNamed:@"用户信息-已关注"] forState:UIControlStateSelected];
    [self.attentionButton addTarget:self action:@selector(clickAttentionOrMsgButton:) forControlEvents:UIControlEventTouchUpInside];
    self.attentionButton.titleLabel.font = [UIFont systemFontOfSize:13 * SCREENH6];
    self.attentionButton.tag = 2001;
    
    [self.topImageView addSubview:self.attentionButton];
    
    
    self.msgButton = [UIButton new];
    
    [self.msgButton setTitle:@"私聊" forState:UIControlStateNormal];
    [self.msgButton setTitleColor:ZRWhiteColor forState:UIControlStateNormal];
    [self.msgButton setBackgroundColor:ZRBlackColor];
    [self.msgButton addTarget:self action:@selector(clickAttentionOrMsgButton:) forControlEvents:UIControlEventTouchUpInside];
    self.msgButton.layer.cornerRadius = 5;
    self.msgButton.layer.masksToBounds = YES;
    self.msgButton.titleLabel.font = [UIFont systemFontOfSize:13 * SCREENH6];
    self.msgButton.tag = 2002;
    
    [self.topImageView addSubview:self.msgButton];
    
    self.attentionValueLabel = [self setupLabelWithTitle:@"5" WithType:@"big"];
    [self.topImageView addSubview:self.attentionValueLabel];
    self.fansValueLabel = [self setupLabelWithTitle:@"3000" WithType:@"big"];
    [self.topImageView addSubview:self.fansValueLabel];
    //    self.comeValueLabel = [self setupLabelWithTitle:@"4000" WithType:@"big"];
    //    [self.topImageView addSubview:self.comeValueLabel];
    
    self.attentionLabel = [self setupLabelWithTitle:@"关注" WithType:@"small"];
    [self.topImageView addSubview:self.attentionLabel];
    self.fansLabel = [self setupLabelWithTitle:@"粉丝" WithType:@"small"];
    [self.topImageView addSubview:self.fansLabel];
    
    
    UIImageView * albumImageView = [UIImageView new];
    albumImageView.image = [UIImage imageNamed:@"用户中心相册"];
    self.albumImageView = albumImageView;
    [self addSubview:self.albumImageView];
    
    UILabel * albumLabel = [UILabel new];
    
    albumLabel.text = @"他的相册";
    albumLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    albumLabel.textColor = ZRBlackColor;
    
    self.albumLabel = albumLabel;
    
    [self addSubview:self.albumLabel];
    
    
    UILabel * albumCountLabel = [UILabel new];
    
    albumCountLabel.textColor = [UIColor lightGrayColor];
    albumCountLabel.text = @"0张照片";
    albumCountLabel.font = [UIFont systemFontOfSize:13.5 * SCREENH6];
    
    self.albumCountLabel = albumCountLabel;
    
    [self addSubview:self.albumCountLabel];
    
    
    UIButton * moreButton = [UIButton new];
    
    [moreButton setTitle:@"查看更多" forState:UIControlStateNormal];
    [moreButton addTarget:self action:@selector(clickMoreButton) forControlEvents:UIControlEventTouchUpInside];
    [moreButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    moreButton.titleLabel.font = [UIFont systemFontOfSize:13 * SCREENH6];
    
    self.moreButton = moreButton;
    
    [self addSubview:self.moreButton];
    
    self.rightImageView = [self setupImageViewWith:@"查看更多"];
    self.rightImageView.hidden = NO;
    self.rightImageView.userInteractionEnabled = NO;
    [self addSubview:self.rightImageView];
    
    self.oneImageView = [self setupImageViewWith:@""];
    [self addSubview:self.oneImageView];
    self.twoImageView = [self setupImageViewWith:@""];
    [self addSubview:self.twoImageView];
    self.threeImageView = [self setupImageViewWith:@""];
    [self addSubview:self.threeImageView];
    
    UILabel * noAlbumLabel = [UILabel new];
    
    noAlbumLabel.text = @"对方尚未上传相片";
    noAlbumLabel.textColor = [UIColor lightGrayColor];
    noAlbumLabel.font = [UIFont systemFontOfSize:17 * SCREENH6];
    noAlbumLabel.textAlignment = NSTextAlignmentCenter;
    noAlbumLabel.hidden = YES;
    
    self.noAlbumLabel = noAlbumLabel;
    
    [self addSubview:self.noAlbumLabel];
    
    UIView * lineView = [UIView new];
    
    lineView.backgroundColor = ColorWithRGB(0xe4e4e4);
    
    self.lineView = lineView;
    
    [self addSubview:self.lineView];
    
    
    UIImageView * dynamicImageView = [UIImageView new];
    dynamicImageView.image = [UIImage imageNamed:@"用户信息-他的动态"];
    self.dynamicImageView = dynamicImageView;
    [self addSubview:self.dynamicImageView];
    
    UILabel * dynamicLabel = [UILabel new];
    
    dynamicLabel.text = @"他的动态";
    dynamicLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    dynamicLabel.textColor = ZRBlackColor;
    
    self.dynamicLabel = dynamicLabel;
    
    [self addSubview:self.dynamicLabel];
    
    
    UILabel * dynamicCountLabel = [UILabel new];
    
    dynamicCountLabel.textColor = [UIColor lightGrayColor];
    dynamicCountLabel.text = @"0个动态";
    dynamicCountLabel.font = [UIFont systemFontOfSize:13.5 * SCREENH6];
    dynamicCountLabel.textAlignment = NSTextAlignmentRight;
    
    self.dynamicCountLabel = dynamicCountLabel;
    
    [self addSubview:self.dynamicCountLabel];
}

- (void)layoutSubviews{
    
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.top.mas_equalTo(self.topImageView.mas_top).offset(-10 * SCREENH6);
        make.height.width.mas_equalTo(60 * SCREENH6);
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.top.mas_equalTo(self.iconImageView.mas_bottom).offset(10 * SCREENH6);
    }];
    
    [self.localLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.nameLabel.mas_bottom).offset(10 * SCREENH6);
        make.centerX.mas_equalTo(self);
    }];
    
    [self.localImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.localLabel);
        make.trailing.mas_equalTo(self.localLabel.mas_leading).offset(-5 * SCREENH6);
        make.width.mas_equalTo(9 * SCREENH6);
        make.height.mas_equalTo(11 * SCREENH6);
    }];
    
    [self.attentionButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.mas_centerX).offset(-10 * SCREENH6);
        make.top.mas_equalTo(self.localImageView.mas_bottom).offset(15 * SCREENH6);
        make.width.mas_equalTo(55 * SCREENH6);
        make.height.mas_equalTo(25 * SCREENH6);
    }];
    
    [self.msgButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.height.width.mas_equalTo(self.attentionButton);
        make.leading.mas_equalTo(self.mas_centerX).offset(10 * SCREENH6);
    }];
    
    [self.fansValueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.msgButton);
        make.top.mas_equalTo(self.attentionButton.mas_bottom).offset(10 * SCREENH6);
    }];
    
    [self.fansLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.fansValueLabel);
        make.top.mas_equalTo(self.fansValueLabel.mas_bottom).offset(3 * SCREENH6);
    }];
    
    [self.attentionValueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.attentionButton);
        make.centerY.mas_equalTo(self.fansValueLabel);
    }];
    
    [self.attentionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.attentionValueLabel);
        make.centerY.mas_equalTo(self.fansLabel);
    }];
    
    [self.albumImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.topImageView.mas_bottom);
        make.leading.mas_equalTo(self).offset(15 * SCREENH6);
        make.width.height.mas_equalTo(60 * SCREENH6);
    }];
    
    [self.albumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.albumImageView.mas_centerY).offset(-3 * SCREENH6);
        make.leading.mas_equalTo(self.albumImageView.mas_trailing).offset(5 * SCREENH6);
    }];
    
    [self.albumCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.albumImageView.mas_centerY).offset(3 * SCREENH6);
        make.leading.mas_equalTo(self.albumLabel);
    }];
    
    [self.rightImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self).offset(-20 * SCREENH6);
        make.centerY.mas_equalTo(self.albumImageView);
        make.width.mas_equalTo(7 * SCREENH6);
        make.height.mas_equalTo(13 * SCREENH6);
    }];
    
    [self.moreButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.rightImageView.mas_leading);
        make.centerY.mas_equalTo(self.rightImageView);
    }];
    
    [self.oneImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.albumImageView);
        make.top.mas_equalTo(self.albumImageView.mas_bottom).offset(10 * SCREENH6);
        make.width.mas_equalTo(110 * SCREENH6);
        make.height.mas_equalTo(80 * SCREENH6);
    }];
    
    [self.twoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.width.height.mas_equalTo(self.oneImageView);
        make.leading.mas_equalTo(self.oneImageView.mas_trailing).offset(5 * SCREENH6);
    }];
    
    [self.threeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.width.height.mas_equalTo(self.twoImageView);
        make.leading.mas_equalTo(self.twoImageView.mas_trailing).offset(5 * SCREENH6);
    }];
    
    [self.noAlbumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.centerY.mas_equalTo(self.oneImageView);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.albumImageView.mas_bottom).offset(105 * SCREENH6);
        make.leading.mas_equalTo(self).offset(15 * SCREENH6);
        make.trailing.mas_equalTo(self).offset(-15 * SCREENH6);
        make.height.mas_equalTo(1 * SCREENH6);
    }];
    
    [self.dynamicImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.width.height.mas_equalTo(self.albumImageView);
        make.top.mas_equalTo(self.lineView.mas_bottom).offset(15 * SCREENH6);
    }];
    
    [self.dynamicLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.dynamicImageView);
        make.leading.mas_equalTo(self.albumLabel);
    }];
    
    [self.dynamicCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self).offset(-15 * SCREENH6);
        make.centerY.mas_equalTo(self.dynamicImageView);
    }];
}


- (void)clickAttentionOrMsgButton:(UIButton *)button{
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(clickTopButton:)])
    {
        [self.delegate clickTopButton:button];
    }
    
}


- (void)clickMoreButton{
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(didMoreButton)])
    {
        [self.delegate didMoreButton];
    }
}

- (void)tapAction:(UITapGestureRecognizer*)sender{
    
    //可以根据手势得到它当前所作用的视图
    UIImageView *imageView = (UIImageView*)sender.view;
    if (self.delegate && [self.delegate respondsToSelector:@selector(clickIconImageWith:)])
    {
        [self.delegate clickIconImageWith:imageView];
    }
}


- (UILabel *)setupLabelWithTitle:(NSString *)title WithType:(NSString *)type{
    
    UILabel * label = [UILabel new];
    
    label.text = title;
    if ([type isEqualToString:@"big"]) {
        
        label.textColor = ZRBlackColor;
        label.font = [UIFont boldSystemFontOfSize:16 * SCREENH6];
    }else if ([type isEqualToString:@"small"]){
        
        label.textColor = [UIColor lightGrayColor];
        label.font = [UIFont systemFontOfSize:13 * SCREENH6];
    }
    
    
    label.textAlignment = NSTextAlignmentCenter;
    
    return label;
}


- (UIImageView *)setupImageViewWith:(NSString *)img{
    
    UIImageView * imageView = [UIImageView new];
    
    imageView.image = [UIImage imageNamed:img];
    imageView.hidden = YES;
    imageView.userInteractionEnabled = YES;
    //创建轻拍手势
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [imageView addGestureRecognizer:tapGR];
    
    return imageView;
}


@end
